package org.csu.chatserver.service.Impl;

import org.csu.chatserver.mapper.UserMapper;
import org.csu.chatserver.entity.dto.LoginDTO;
import org.csu.chatserver.entity.dto.RegisterDTO;
import org.csu.chatserver.entity.pojo.User;
import org.csu.chatserver.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


//完成具体的逻辑
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public String register(RegisterDTO registerDTO) {
        User user = new User();
        BeanUtils.copyProperties(registerDTO, user);
        if(userMapper.getUserByName(user.getUsername())==null){
            userMapper.insert(user);
            System.out.println("注册成功");
            return "注册成功";
        }else {
            System.out.println("用户名已存在");
            return "用户名已存在";
        }
    }

    @Override
    public String login(LoginDTO loginDTO) {
        User user = new User();
        BeanUtils.copyProperties(loginDTO, user);
        String password =userMapper.getPasswordByName(user.getUsername());
        if(password!=null && password.equals(loginDTO.getPassword())) {
            return userMapper.getIdByName(user.getUsername());
        }
        return null;
    }

    @Override
    public List<User> getUserList() {
        return userMapper.selectList(null);
    }
}
